package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

@RestController
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    //套餐分页查询
    @GetMapping("/setmeal/page")
    public ResultInfo getPage(String name, @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
                              @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
       Page<Setmeal> page= setmealService.getPage(name,pageNum,pageSize);
        return ResultInfo.success(page);
    }
    //添加套餐
   @PostMapping("/setmeal")
   @CacheEvict(value = "setmeal",allEntries = true)//变动数据时 删除redis缓存数据
    public ResultInfo save(@RequestBody Setmeal setmeal){
        setmealService.save(setmeal);
        return ResultInfo.success(null);
    }
    //删除套餐
    @DeleteMapping("/setmeal")
    @CacheEvict(value = "setmeal",allEntries = true)//变动数据时 删除redis缓存数据
    public ResultInfo delete(Long[] ids) {
        if (ids != null && ids.length > 0) {
            setmealService.deleteByIds(ids);
        }
        return ResultInfo.success(null);
    }
    //禁用
    @CacheEvict(value = "setmeal",allEntries = true)//变动数据时 删除redis缓存数据
    @PostMapping("/setmeal/status/{status}")
    public ResultInfo setMyStatus(@PathVariable("status")Integer status,Long[] ids){
        if(ids!=null && ids.length>0) {
            setmealService.setMyStatus(status, ids);
        }
            return ResultInfo.success(null);
        }
    //回显套餐信息
    @GetMapping("/setmeal/{id}")
    public ResultInfo findById(@PathVariable("id") Long id) {
        Setmeal setmeal = setmealService.findById(id);
        return ResultInfo.success(setmeal);
    }

    //修改
    @PutMapping("/setmeal")
    @CacheEvict(value = "setmeal",allEntries = true)//变动数据时 删除redis缓存数据
    public ResultInfo update(@RequestBody Setmeal setmeal) {
        setmealService.update(setmeal);
        return ResultInfo.success(null);
    }


}
